package com.zy.studentSide.controller;

import com.zy.studentSide.entity.*;
import com.zy.studentSide.entity.analysis.CourseCreditAnalysis;
import com.zy.studentSide.entity.analysis.StudentAchievementAnalysis;
import com.zy.studentSide.entity.analysis.TaskSubmit;
import com.zy.studentSide.entity.dto.CourseTaskState;
import com.zy.studentSide.entity.dto.GenderRatio;
import com.zy.studentSide.entity.analysis.StudentAgeAnalysis;
import com.zy.studentSide.service.IStudentCourseService;
import com.zy.studentSide.service.IStudentService;
import com.zy.studentSide.service.IStudentTaskService;
import com.zy.studentSide.service.Impl.IStudentInformationServiceImpl;
import com.zy.teacherSide.entity.Classroom;
import com.zy.teacherSide.entity.Course;
import com.zy.teacherSide.entity.StudentTaskAchievement;
import com.zy.teacherSide.entity.TeacherTask;
import com.zy.teacherSide.entity.ov.CourseOV;
import com.zy.teacherSide.entity.ov.TaskOV;
import com.zy.teacherSide.service.*;
import com.zy.util.DateUtil;
import com.zy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 29787
 */
@RestController
public class EChartsDataController {

    @Autowired
    private IClassroomService iClassroomService;

    @Autowired
    private ICourseService iCourseService;

    @Autowired
    private IStudentCourseService iStudentCourseService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private IStudentInformationServiceImpl iStudentInformationService;

    @Autowired
    private ITeacherTaskService iTeacherTaskService;

    @Autowired
    private IStudentTaskService iStudentTaskService;

    @Autowired
    private IStudentTaskAchievementService iStudentTaskAchievementService;

    /**
     * 教师获取所选班级下的课程下的学生的男女比例数据
     *
     * @param teacherId
     * @param tov
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherGetGenderRatioEChartsData/{teacherId}")
    public R teacherGetGenderRatioEChartsData(@PathVariable Integer teacherId, @RequestBody TaskOV tov) throws ParseException {

        T t = new T();

        // 获取班级信息
        Classroom classroom = iClassroomService.getClassroomByNameAndTeacherId(tov.getClassroomName(), teacherId);
        // 获取课程信息
        Course course = iCourseService.getCourseByCourseNameAndTeacherId(tov.getCourseName(), teacherId);
        // 获取学生课程信息
        List<StudentCourse> sc_list = iStudentCourseService.getStudentCourseByCourseIdAndTeacherIdAndClassroomId(course.getCourseId(), teacherId, classroom.getClassroomId());

        // 封装男女比例数据
        GenderRatio gr = new GenderRatio();
        int male = 0;
        int female = 0;
        for (StudentCourse sc : sc_list) {
            // 获取学生信息
            Student stu = iStudentService.getStudentByStudentId(sc.getStudentId());
            // 获取学生基本信息
            StudentInformation si = iStudentInformationService.getStudentInformationByStudentNum(stu.getStudentNum());
            // 判断学生性别
            if ("男".equals(si.getStudentSex())) {
                male++;
            } else if ("女".equals(si.getStudentSex())) {
                female++;
            }
            gr.setMale(male);
            gr.setFemale(female);
        }

        // 获取教师课程作业信息
        List<TeacherTask> tt_list = iTeacherTaskService.getTeacherTaskByClassroomIdAndTeacherIdAndCourseId(classroom.getClassroomId(), teacherId, course.getCourseId());
        // 封装作业三状态数据
        CourseTaskState cts = new CourseTaskState();
        int underway = 0;
        int noStart = 0;
        int hasEnd = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (TeacherTask tt : tt_list) {
            // 今天的时间
            Date today = new Date();
            if (DateUtil.comparisonDate(sdf.parse(tt.getStartTime()), today)) {
                noStart++;
            } else if (DateUtil.comparisonDate(today, sdf.parse(tt.getEndTime()))) {
                hasEnd++;
            } else {
                underway++;
            }
            cts.setUnderway(underway);
            cts.setNoStart(noStart);
            cts.setHasEnd(hasEnd);
        }

        t.setGenderRatio(gr);
        t.setCourseTaskState(cts);
        return new R(true, t);
    }

    /**
     * 教师获取学生的姓名和年龄数据
     *
     * @param teacherId
     * @param tov
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherGetStudentNameAndAgeData/{teacherId}")
    public R teacherGetStudentNameAndAgeData(@PathVariable Integer teacherId, @RequestBody TaskOV tov) {

        // 获取班级信息
        Classroom classroom = iClassroomService.getClassroomByNameAndTeacherId(tov.getClassroomName(), teacherId);
        // 获取课程信息
        Course course = iCourseService.getCourseByCourseNameAndTeacherId(tov.getCourseName(), teacherId);
        // 获取学生课程信息
        List<StudentCourse> sc_list = iStudentCourseService.getStudentCourseByCourseIdAndTeacherIdAndClassroomId(course.getCourseId(), teacherId, classroom.getClassroomId());

        StudentAgeAnalysis a = new StudentAgeAnalysis();
        String[] name = new String[sc_list.size()];
        Integer[] age = new Integer[sc_list.size()];
        for (int i = 0; i < sc_list.size(); i++) {
            // 获取学生信息
            Student stu = iStudentService.getStudentByStudentId(sc_list.get(i).getStudentId());
            // 获取学生基本信息
            StudentInformation si = iStudentInformationService.getStudentInformationByTeacherId(stu.getStudentNum(), teacherId);

            name[i] = si.getStudentName();
            age[i] = si.getStudentAge();

        }
        a.setStudentName(name);
        a.setStudentAge(age);
        return new R(true, a);
    }

    /**
     * 教师获取课程的课程名称和课程学分数据
     *
     * @param teacherId
     * @param tov
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherCourseCreditAnalysis/{teacherId}")
    public R teacherCourseCreditAnalysis(@PathVariable Integer teacherId, @RequestBody TaskOV tov) {

        // 获取班级信息
        Classroom classroom = iClassroomService.getClassroomByNameAndTeacherId(tov.getClassroomName(), teacherId);

        // 查询课程信息
        List<Course> c_list = iCourseService.getAllCourseByTeacherIdAndClassroomId(teacherId, classroom.getClassroomId());

        CourseCreditAnalysis cca = new CourseCreditAnalysis();

        String[] name = new String[c_list.size()];
        Integer[] credit = new Integer[c_list.size()];

        for (int i = 0; i < c_list.size(); i++) {
            name[i] = c_list.get(i).getCourseName();
            credit[i] = c_list.get(i).getCredit();
        }
        cca.setCourseName(name);
        cca.setCourseCredit(credit);
        return new R(true, cca);
    }

    /**
     * 教师获取班级下课程下某次作业提交情况数据
     *
     * @param teacherId
     * @param cov
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherGetSubmitCourseAssignmentData/{teacherId}")
    public R teacherGetSubmitCourseAssignmentData(@PathVariable Integer teacherId, @RequestBody CourseOV cov) {

        // 获取班级信息
        Classroom classroom = iClassroomService.getClassroomByNameAndTeacherId(cov.getClassroomName(), teacherId);
        // 获取课程信息
        Course course = iCourseService.getCourseByCourseNameAndTeacherId(cov.getCourseName(), teacherId);
        // 获取作业信息
        TeacherTask tt = iTeacherTaskService.getTeacherTaskByCourseIdAndTaskNameAndTeacherIdAndClassroomId(course.getCourseId(), cov.getTaskName(), teacherId, classroom.getClassroomId());

        if (tt != null) {
            // 获取学生作业信息(已交次子作业)
            List<StudentTask> st_list = iStudentTaskService.getStudentTaskById(tt.getClassroomId(), tt.getTaskId(), teacherId, tt.getCourseId());
            // 获取学生课程信息(应交此次作业)
            List<StudentCourse> sc_list = iStudentCourseService.getStudentCourseByCourseIdAndTeacherIdAndClassroomId(tt.getCourseId(), teacherId, tt.getClassroomId());


            Integer[] paid = new Integer[sc_list.size()];
            Integer[] unpaid = new Integer[sc_list.size()];

            // 封装已交此次作业的人数及学生名单
            for (int i = 0; i < sc_list.size(); i++) {
                paid[i] = sc_list.get(i).getStudentId();
            }
            // 封装未交此次作业的人数及学生名单
            for (int i = 0; i < st_list.size(); i++) {
                unpaid[i] = st_list.get(i).getStudentId();
            }

            TaskSubmit ts = new TaskSubmit();
            ts.setTotal(sc_list.size());
            ts.setPaid(st_list.size());
            ts.setUnpaid(sc_list.size() - st_list.size());
            return new R(true, ts);
        } else {
            return new R(false, null);
        }
    }

    /**
     * 教师获取班级下课程下某次作业的学生成绩数据
     *
     * @param teacherId
     * @param cov
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherGetStudentAchievementData/{teacherId}")
    public R teacherGetStudentAchievementData(@PathVariable Integer teacherId, @RequestBody CourseOV cov) {

        // 获取班级信息
        Classroom classroom = iClassroomService.getClassroomByNameAndTeacherId(cov.getClassroomName(), teacherId);
        // 获取课程信息
        Course course = iCourseService.getCourseByCourseNameAndTeacherId(cov.getCourseName(), teacherId);
        // 获取作业信息
        TeacherTask tt = iTeacherTaskService.getTeacherTaskByCourseIdAndTaskNameAndTeacherIdAndClassroomId(course.getCourseId(), cov.getTaskName(), teacherId, classroom.getClassroomId());

        if (tt == null){
            return new R(false,null);
        } else {
            List<StudentTaskAchievement> sta_list = iStudentTaskAchievementService.getStudentTaskAchievementByCourseIdAndTaskIdAndClassroomIdAndTeacherId(course.getCourseId(), tt.getTaskId(), teacherId, classroom.getClassroomId());

            Integer[] stu_id = new Integer[sta_list.size()];
            Integer[] achievement = new Integer[sta_list.size()];
            Integer[] achievementSection = new Integer[6];
            String[] name = new String[sta_list.size()];

            for(int i = 0;i < sta_list.size();i++){
                stu_id[i] = sta_list.get(i).getStudentId();
                achievement[i] = sta_list.get(i).getAchievement();
            }

            for (int i = 0;i < stu_id.length;i++){
                Student s = iStudentService.getStudentByStudentId(stu_id[i]);
                name[i] = s.getStudentName();
            }

            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int e = 0;
            int f = 0;
            for(int i = 0;i < achievement.length;i++){
                if(achievement[i] >= 0 && achievement[i] <= 59){
                    a++;
                } else if(achievement[i] >= 60 && achievement[i] <= 69){
                    b++;
                }else if(achievement[i] >= 70 && achievement[i] <= 79){
                    c++;
                }else if(achievement[i] >= 80 && achievement[i] <= 89){
                    d++;
                }else if(achievement[i] >= 90 && achievement[i] <= 99){
                    e++;
                }else if(achievement[i] == 100){
                    f++;
                }
            }

            achievementSection[0] = a;
            achievementSection[1] = b;
            achievementSection[2] = c;
            achievementSection[3] = d;
            achievementSection[4] = e;
            achievementSection[5] = f;

            StudentAchievementAnalysis saa = new StudentAchievementAnalysis();
            saa.setStudentName(name);
            saa.setStudentAchievement(achievement);
            saa.setAchievementSection(achievementSection);

            return new R(true,saa);
        }

    }

}
